import {constructBeatMapParser} from "./BeatMap/BeatMapParserConstructor"

const fs = wx.getFileSystemManager()

class BSFormatParser {
  constructor(item, data) {
    this.item = item
    this.data = data;
    this.diffs = {}
  }

  getSongLength(){
    return this.item["metadata"]["duration"]
  }

  getSongFileName() {
    throw new Error("Method 'getSongFileName()' must be implemented.");
  }

  getRequirements(){
    return []
  }

  getRawBpm(){
    throw new Error("Method 'getRawBpm()' must be implemented.");
  }

  loadDiffFile(diffName){
    if(this.diffs[diffName]) // This diff has already been loaded.
      return this.diffs[diffName]
    const targetPath = getApp().globalData.songs[this.item.id].path
    const diffFileName = targetPath + this.getDiffFile(diffName)
    console.log("DiffFile:" + diffFileName)
    try {
      const data = fs.readFileSync(diffFileName, 'utf-8')
      const jsonData = JSON.parse(data)
      this.diffs[diffName] = constructBeatMapParser(this, jsonData)
      console.log(jsonData)
      return this.diffs[diffName]
    }catch(e){
      console.log("Error loading diff:", e)
    }
  }
}

export {BSFormatParser}